package util;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class oracleConnector {
    public final static String OralceDsJNDI = "dishuiDS";

    public Connection getConnection() throws SQLException {
        /*
		javax.sql.DataSource ds = null;  
		
		try {
			Hashtable<String, String> env = new Hashtable<String, String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"weblogic.jndi.WLInitialContextFactory");
			env.put(Context.PROVIDER_URL, "t3://localhost:7001/");
			Context initialContext = new InitialContext(env);
			ds = (javax.sql.DataSource) initialContext   
					.lookup(OpenDbBean.OralceDsJNDI);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	
		javax.sql.DataSource ds = null;
		try {
			Context ctx = new InitialContext(); 
		    ds = (DataSource)ctx.lookup("java:comp/env/jdbc/dishui");  
		} catch (NamingException e) {    
			e.printStackTrace();
		}

		
		
		
		
		return ds.getConnection();
		*/
        javax.sql.DataSource ds = null;
        try {
            String userName = "dishui";
            String userPassword = "dishui";
            String url = "jdbc:oracle:thin:@127.0.0.1:1521:dishui";
            Class.forName("oracle.jdbc.driver.OracleDriver");
            return DriverManager.getConnection(url, userName, userPassword);
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public void CleanConnection(Connection con, Statement ps, ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
        // connMgr.freeConnection("idb", con);
        // connMgr.release();
    }

    public void CleanConnection(Connection con, PreparedStatement ps, ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
        // connMgr.freeConnection("idb", con);
        // connMgr.release();
    }

    public void CleanConnection(Connection con, CallableStatement ps, ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
        // connMgr.freeConnection("idb", con);
        // connMgr.release();
    }

    public void CleanConnection(Connection con, CallableStatement ps) throws SQLException {
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
        // connMgr.freeConnection("idb", con);
        // connMgr.release();
    }

    public void CleanConnection(Connection con, PreparedStatement ps) throws SQLException {
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
        // connMgr.freeConnection("idb", con);
        // connMgr.release();
    }

    public void CleanConnection(Connection con) throws SQLException {
        // connMgr.freeConnection("idb", con);
        // connMgr.release();
        if (con != null) {
            con.close();
        }
    }

}